import { loadUsersJson,saveUser,deleteUser,editUserInfoNew,resetUserPassword} from '@/services/geographic';
import { queryCurrent,getRobotList,delRobot,addRobot,updateRobot,changeUser,getmenusByUserId,info} from '@/services/user';
import { setAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils';
import { reloadAuthorized } from '@/utils/Authorized';
import { routerRedux } from 'dva/router';
export default {
  namespace:'robotManage',
  state: {
    total:0,
    items:[],
    loading:true
  },
  effects: {
    *getUsers({ payload, callback }, { call, put }) {
      const response = yield call(getRobotList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addUser({ payload, callback }, { call, put }) {
      const response = yield call(addRobot, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *editUser({ payload, callback }, { call, put }) {
      const response = yield call(updateRobot, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *resetUser({ payload, callback }, { call, put }) {
      const response = yield call(resetUserPassword, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *delUser({ payload, callback }, { call, put }) {
      const response = yield call(delRobot, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *queryUser({ payload, callback }, { call, put }) {
      const response = yield call(queryCurrent, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *changeUserByAdmin({ payload, callback }, { call, put }) {
      const response = yield call(changeUser, payload);
      if(response){
        // Login successfully
        window.localStorage.setItem("robot_token", response.token);
        yield put({
          type: 'changeLoginStatus',
          payload: response.token
        });
        const response1 = yield call(info, {});
        yield put({
          type: 'saveAuth',
          payload: response1.menus,
        });
        if (response1) {
          reloadAuthorized();
          window.localStorage.setItem("currentUser",JSON.stringify(response1));
          const urlParams = new URL(window.location.href);
          const params = getPageQuery();
          let { redirect } = params;
          if (redirect) {
            const redirectUrlParams = new URL(redirect);
            if (redirectUrlParams.origin === urlParams.origin) {
              redirect = redirect.substr(urlParams.origin.length);
              if (redirect.startsWith('/#')) {
                redirect = redirect.substr(2);
              }
            } else {
              window.location.href = redirect;
              return;
            }
          }
          yield put(routerRedux.replace(redirect || '/'));
          window.location.reload()
        }
      }
      // const response = yield call(changeUser, payload);
      // const response1 = yield call(info);
      // // 删除缓存的用户信息
      // window.localStorage.removeItem("currentUser");
      // // 删除缓存的用户等级
      // window.localStorage.removeItem("antd-pro-authority");
      // //删除token
      // window.localStorage.removeItem("token");
      // //保存token
      // if (response) {
      //   window.localStorage.setItem("token",response.token);
      //   yield put({
      //     type: 'save',
      //     payload: response,
      //   });
      // }
      // if (response1) {
      //   window.localStorage.setItem("currentUser",JSON.stringify(response1) );
      //   yield put({
      //     type: 'saveAuth',
      //     payload: response1,
      //   });
      //   reloadAuthorized();
      //   yield put(routerRedux.replace('/'));
      //   window.location.reload()
      // }
      

      // if(response1){
      //   yield put({
      //     type: 'saveAuth',
      //     payload: response1,
      //   });
      //   reloadAuthorized();
      //   yield put(routerRedux.replace('/'));
      //   window.location.reload()
      // }
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        ...state,
        token:  payload,
        loading: false
      };
    },
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveAuth(state, { payload }) {
      setAuthority(payload);
      return {
        ...state,
        ...payload,
      };
    }
  },
};
